package com.example.stardemo;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.DialogFragment;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


public class CardDialogFragment extends DialogFragment {

    private final int starId;
    private final List<String> storyList = new ArrayList<>();
    private final int[] cardImgId = {
            R.drawable.wz0,
            R.drawable.wz1,
            R.drawable.wz2,
            R.drawable.wz3,
            R.drawable.wz4,
            R.drawable.wz5,
            R.drawable.wz6,
            R.drawable.wz7,
            R.drawable.wz8,
            R.drawable.wz9};


    public CardDialogFragment(int starId) {
        this.starId = starId;
        loadStory();
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Objects.requireNonNull(getDialog()).getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View v = inflater.inflate(R.layout.card_dialog_fragment,container,false);
        //根据传入的星球id获取当前碎片要展示的故事
        String storyString = storyList.get(starId);
        TextView tv = v.findViewById(R.id.story_text_view);
        tv.setText(storyString);
        // //根据传入的星球id获取当前碎片要展示的卡片
        CardView cardView = v.findViewById(R.id.card);
        ImageView imageView = v.findViewById(R.id.card_img);
        imageView.setImageResource(cardImgId[starId]);
        //卡片渐变旋转入场动画
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(cardView,"rotationY",0,180);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(cardView,"scaleX",0,1);
        ObjectAnimator animator4 = ObjectAnimator.ofFloat(cardView,"scaleY",0,1);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(cardView,"alpha",0,1);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(animator1,animator2,animator3,animator4);
        animatorSet.setDuration(1000);
        animatorSet.start();
        //取消按钮
        v.findViewById(R.id.cancel).setOnClickListener(v1 -> dismiss());
        return v;
    }





    private void loadStory() {
        //应该从文件或者JSON中读取
        String s0 = "story0_para0";
        String s1 = "story1_para1";
        String s2 = "story2_para2";
        String s3 = "story3_para0";
        String s4 = "story4_para1";
        String s5 = "story5_para1";
        String s6 = "story6_para1";
        String s7 = "story7_para1";
        String s8 = "story8_para1";
        String s9 = "story9_para1";
        storyList.add(s0);
        storyList.add(s1);
        storyList.add(s2);
        storyList.add(s3);
        storyList.add(s4);
        storyList.add(s5);
        storyList.add(s6);
        storyList.add(s7);
        storyList.add(s8);
        storyList.add(s9);
    }


}